<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="单据号" prop="ticketNo">
        <el-input
          v-model="queryParams.ticketNo"
          placeholder="请输入单据号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['assets:storagereceipt:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['assets:storagereceipt:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['assets:storagereceipt:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['assets:storagereceipt:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="storagereceiptList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope"
          >{{
            scope.$index + 1 + (queryParams.pageNum - 1) * queryParams.pageSize
          }}
        </template>
      </el-table-column>
      <el-table-column label="单据号" align="center" prop="ticketNo" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="类型" align="center" prop="stype">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.warehouse_in_type"
            :value="scope.row.stype"
          />
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['assets:storagereceipt:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['assets:storagereceipt:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-select v-model="form.warehouseName" placeholder="请选择仓库" ref="warehouse">
            <el-option
              v-for="item in warehouseOptions"
              :key="item.warehouseID"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="stype">
          <el-select v-model="form.stype" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.warehouse_in_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入说明" />
        </el-form-item>
      </el-form>

      <div>
        <el-row style="margin-bottom: 10px">
          <el-col>
            <el-button @click="addotherlist">添加</el-button>
          </el-col>
        </el-row>
        <el-table :data="otherlist" border :rules="rules">
          <el-table-column label="序号" type="index" align="center" width="50">
          </el-table-column>
          <el-table-column
            prop="operation"
            label="操作"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <div>
                <el-button
                  icon="el-icon-delete"
                  v-model="scope.row.operation"
                  @click="deletelist(scope.row)"
                  >删除</el-button
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="assetsname"
            label="资产名称"
            align="center"
            width="150"
          >
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.assetsname"
                placeholder="请选择资产名称"
              >
                <el-option
                  v-for="item in assetsOptions"
                  :key="item.devNo"
                  :label="item.name"
                  :value="item.name"
                >
                </el-option> </el-select
            ></template>
          </el-table-column>
          <el-table-column
            prop="serialnumber"
            label="序列号"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <div>
                <el-input
                  v-model="scope.row.serialnumber"
                  placeholder="请输入序列号"
                ></el-input>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="tagcode"
            label="标签码(RFID模块必须)"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <div>
                <el-input
                  v-model="scope.row.tagcode"
                  placeholder="请输入标签码"
                ></el-input>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="SIMID"
            label="SIM卡号(GPS模块必须)"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <div>
                <el-input
                  v-model="scope.row.SIMID"
                  placeholder="请输入SIM卡号"
                ></el-input>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-button @click="sendotherlist"> 发送 </el-button>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStoragereceipt,
  getStoragereceipt,
  delStoragereceipt,
  addStoragereceipt,
  updateStoragereceipt,
} from "@/api/assets/storagereceipt";

export default {
  name: "Storagereceipt",
  dicts: ["warehouse_in_type"],
  data() {
    return {
      otherlistid: 0,
      otherlist: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库管理表格数据
      storagereceiptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 仓库选项
      warehouseOptions: [],
      //资产名称选项
      assetsOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ticketNo: null,
        warehouseName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" },
        ],
        stype: [{ required: true, message: "类型不能为空", trigger: "blur" }],
        assetsname: [
          { required: true, message: "资产名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //新增资产
    addotherlist() {
      this.otherlistid = this.otherlistid + 1;
      let tempid = this.otherlistid;
      this.otherlist.push({
        id: tempid,
        operation: "",
        serialnumber: "",
        assetsname: "",
        tagcode: "",
        SIMID: "",
      });
    },
    //删除资产
    deletelist(myid) {
      this.otherlist = this.otherlist.filter((item) => item.id != myid.id);
    },

    sendotherlist() {
      console.log("this.otherlist :>> ", this.otherlist);
      this.otherlist = [];
    },
    /** 查询入库管理列表 */
    getList() {
      this.loading = true;
      listStoragereceipt(this.queryParams).then((response) => {
        this.storagereceiptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storageReceiptid: null,
        ticketNo: null,
        warehouseID: null,
        warehouseName: null,
        stype: null,
        remark: null,
        createUsername: null,
        createUserid: null,
        createDate: null,
        editUserid: null,
        editUsername: null,
        editDate: null,
        type: null,
      };
      this.otherlist = [];
      this.otherlistid = 0;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.storageReceiptid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getStoragereceipt().then((response) => {
        this.warehouseOptions = response.warehouseList;
        this.assetsOptions = response.assetsList;
        this.open = true;
        this.title = "添加入库管理";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const storageReceiptid = row.storageReceiptid || this.ids;
      getStoragereceipt(storageReceiptid).then((response) => {
        this.form = response.data;
        this.warehouseOptions = response.warehouseList;
        this.assetsOptions = response.assetsList;
        this.open = true;
        this.title = "修改入库管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // this.form.warehouseId=this.$refs.warehouse.selectdKey;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.storageReceiptid != null) {
            updateStoragereceipt(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStoragereceipt(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storageReceiptids = row.storageReceiptid || this.ids;
      this.$modal
        .confirm(
          '是否确认删除入库管理编号为"' + storageReceiptids + '"的数据项？'
        )
        .then(function () {
          return delStoragereceipt(storageReceiptids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "assets/storagereceipt/export",
        {
          ...this.queryParams,
        },
        `storagereceipt_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
